//
//  LTScrollView.m
//  LTScrollView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 xalo. All rights reserved.
//

#import "LTScrollView.h"
#define kWid self.frame.size.width
#define kHei self.frame.size.height
@interface LTScrollView()<UIScrollViewDelegate>
/**
 *  滚动视图
 */
@property (nonatomic, strong)UIScrollView *scrollView;

/**
 *  左图片
 */
@property (nonatomic, strong)UIImageView *leftImageView;
/**
 *  右图片
 */
@property (nonatomic, strong)UIImageView *rightImageView;
/**
 *  中间图片
 */
@property (nonatomic, strong)UIImageView *moddileImage;
/**
 *  图片数组
 */
@property (nonatomic, strong)NSArray *imageArray;
/**
 *  右边图片下标
 */
@property (nonatomic, assign)NSInteger rightIndex;
/**
 *  左边图片下标
 */
@property (nonatomic, assign)NSInteger leftIndex;

/**
 *  小圆点
 */
@property (nonatomic, strong)UIPageControl *pageControl;
/**
 *  定时器
 */
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation LTScrollView

//小圆点懒加载
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, kHei - 30, 175, 30)];
        _pageControl.numberOfPages = self.imageArray.count;
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    }
    return _pageControl;
}

//滚动视图懒加载
- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        // 开始创建滚动视图及属性
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWid, kHei)];
        _scrollView.contentSize = CGSizeMake(kWid * self.imageArray.count, kHei);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
#warning 初始的scrollView的偏移量
        _scrollView.contentOffset = CGPointMake(kWid, 0);
        
    }
    return _scrollView;
}

//左边视图懒加载
- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        
        //创建两个图片
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWid, kHei)];
        if (self.imageArray.count >0) {
            
        
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.imageArray.count - 1]] placeholderImage:[UIImage imageNamed:@"1"]];
        }}
    return _leftImageView;
}

//右边视图懒加载
- (UIImageView *)rightImageView {
    
    if (!_rightImageView) {
        
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWid * 2, 0, kWid, kHei)];
        if (self.imageArray.count >0) {
            
        
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]] placeholderImage:[UIImage imageNamed:@"1"]];
    }}
    return _rightImageView;
}

- (UIImageView *)moddileImage {
    if (!_moddileImage) {
        
        _moddileImage = [[UIImageView alloc] initWithFrame:CGRectMake(kWid , 0, kWid, kHei)];
        if (self.imageArray.count >0) {
            
        
        [_moddileImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@"1"]];
        }}
    
    return _moddileImage;
}

//初始化方法
- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //给数组赋值
        self.imageArray = imageArray;
        
        self.leftIndex = self.imageArray.count - 1;
        
        self.rightIndex = 1;
        
        //设置视图
        [self creatScrollView];
        
    }
    return self;
}

- (void)setImages:(NSMutableArray *)images {
    
    self.imageArray = images;
    self.rightIndex = 1;
    self.leftIndex = self.imageArray.count - 1;
    self.pageControl.numberOfPages = images.count;

}

- (void)creatScrollView {
    
    [self startTimer];
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    [self.scrollView addSubview:self.moddileImage];
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
}

#pragma mark ---scrollView协议---
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //#define kWidth self.frame.size.width
    //scrollView的偏移量为kWidth * 2 代表的是往右偏移
    if (scrollView.contentOffset.x == kWid * 2) {
        
        scrollView.contentOffset = CGPointMake(kWid, 0);
        
        self.pageControl.currentPage = self.rightIndex;
        
        self.rightIndex++;

        if (self.rightIndex == self.imageArray.count) {
            
            self.rightIndex = 0;
            
        }
        self.moddileImage.image = self.rightImageView.image;
        if (self.imageArray.count>0) {
            
        
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.rightIndex]]];
        }
//        LTLog(@"--右边照片－－－－%ld", self.rightIndex);
        
        //左边照片比右边照片在数组中的下标少2
        self.leftIndex = self.rightIndex - 2;
        
        //往右偏移时左右imageView 的image 都要随时变更
        //当左边照片下标为－1时，代表的是：到数组最后一张照片
        if (self.leftIndex ==  -1) {
            self.leftIndex = self.imageArray.count - 1;
            
        }else if (self.leftIndex == -2) {//当左边照片下标为－2时，代表的是：到数组倒数第二张照片
            self.leftIndex = self.imageArray.count - 2;
        }
        if (self.imageArray.count >0) {
            
        
        
            [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.leftIndex]]];}
        
//        LTLog(@"----左边照片----%ld", self.leftIndex);
    }
    //scrollView的偏移量为0 代表的是往左偏移
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake(kWid, 0);
        
        self.pageControl.currentPage = self.leftIndex;
        
        self.leftIndex--;
        
        if (self.leftIndex < 0) {
            self.leftIndex = self.imageArray.count - 1;
        }
        
//        LTLog(@"----左边照片----%ld", self.leftIndex);
        self.moddileImage.image = self.leftImageView.image;
        if (self.imageArray.count >0) {
            
        
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.leftIndex]]];
        }
        //右边照片比左边照片在数组中的下标多2
        self.rightIndex = self.leftIndex + 2;
        
        //往左偏移时左右imageView 的image 都要随时变更
        //当右边照片下标为数组个数时，代表的是：到数组第一张照片
        if (self.rightIndex == self.imageArray.count) {
            self.rightIndex = 0;
        }else if (self.rightIndex == self.imageArray.count + 1) {//当右边照片下标为数组个数 ＋ 1时，代表的是：到数组第二张照片
            self.rightIndex = 1;
        }
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.rightIndex]]];
       
//        LTLog(@"--右边照片－－－－%ld", self.rightIndex);
        
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
//    LTLog(@"开始拖拽");
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}

#pragma  mark ---TimerAction方法--

- (void)timerAction {
    
    [self.scrollView setContentOffset:CGPointMake(kWid * 2, 0) animated:YES];
}

- (void)stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)startTimer {
    
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    //	[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}




@end
